#include <stdio.h>
#include <queue>
using namespace std;

int m, n;
int adjlist[200010][3];
priority_queue< pair<int, int> > pQueue;
int parent[200010];
int rank[200010];

int getset(int x) {
	while (parent[x] != x) {
		x=parent[x];
	}

	return parent[x];
}

bool unionfind(int a, int b) {
	a = getset(a);
	b = getset(b);

	if (a == b) return false;

	if (rank[a]>=rank[b]) {
		parent[b]=a;
		if (rank[a] == rank[b]) rank[a]++;
	}
	else parent[a]=b;

	return true;
}

int cost;
int maxCost;
int edges;
int ind, u, v;

int main() {
	scanf("%d %d", &m, &n);

	while (m || n) {
		while (!pQueue.empty()) {
			pQueue.pop();
		}

		maxCost = 0;

		for (int i=0; i<n; i++) { 
			scanf("%d %d %d", &adjlist[i][0], &adjlist[i][1], &adjlist[i][2]);
			maxCost+=adjlist[i][2];
		}

		cost=0;

		for (int i=0; i<m; i++) {
			parent[i]=i;
			rank[i]=0;
		}	

		for (int i=0; i<n; i++)
			pQueue.push(make_pair(-adjlist[i][2], i));

		edges=0;

		for (int i=0; i<n; i++) {
			ind = pQueue.top().second;
			pQueue.pop();
			u = adjlist[ind][0];
			v = adjlist[ind][1];

			if (unionfind(u, v))
				cost+=adjlist[ind][2];
		}	

		printf("%d\n", maxCost-cost);

		scanf("%d %d", &m, &n);
	}

	return 0;
}
